<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="large" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="lRealName">
        <el-input
          v-model="queryParams.lRealName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="lNickName">
        <el-input
          v-model="queryParams.lNickName"
          placeholder="请输入昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="lPhoneNumber">
        <el-input
          v-model="queryParams.lPhoneNumber"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="lEmail">
        <el-input
          v-model="queryParams.lEmail"
          placeholder="请输入邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证" prop="lIdCard">
        <el-input
          v-model="queryParams.lIdCard"
          placeholder="请输入身份证"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出生日期" prop="lBirthday">
        <el-date-picker clearable
          v-model="queryParams.lBirthday"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择出生日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="户主id" prop="lPillar">
        <el-input
          v-model="queryParams.lPillar"
          placeholder="请输入户主id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['life:friends:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['life:friends:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['life:friends:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['life:friends:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="friendsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户id" align="center" prop="lUserId" />
      <el-table-column label="姓名" align="center" prop="lRealName" />
      <el-table-column label="昵称" align="center" prop="lNickName" />
      <el-table-column label="手机号" align="center" prop="lPhoneNumber" />
      <el-table-column label="邮箱" align="center" prop="lEmail" />
      <el-table-column label="身份证" align="center" prop="lIdCard" />
      <el-table-column label="出生日期" align="center" prop="lBirthday" width="180">
        <template #default="scope">
            <span>{{ parseTime(scope.row.lBirthday,'{y}-{m}-{d}')}}</span>
         </template>
      </el-table-column>
      <el-table-column prop="lSex" label="用户性别" width="80" align="center">
        <template #default="scope">
          <dict-tag :options="sys_user_sex" :value="scope.row.lSex"/>
        </template>
      </el-table-column>
      <el-table-column label="户主id" align="center" prop="lPillar" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作"  width="180" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
               <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['life:friens:edit']">修改</el-button>
               <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['ife:friens:remove']">删除</el-button>
            </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改亲戚朋友对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="friendsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="lRealName">
          <el-input v-model="form.lRealName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="昵称" prop="lNickName">
          <el-input v-model="form.lNickName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="手机号" prop="lPhoneNumber">
          <el-input v-model="form.lPhoneNumber" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="邮箱" prop="lEmail">
          <el-input v-model="form.lEmail" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="身份证" prop="lIdCard">
          <el-input v-model="form.lIdCard" placeholder="请输入身份证" />
        </el-form-item>
        <el-form-item label="出生日期" prop="lBirthday">
          <el-date-picker clearable
            v-model="form.lBirthday"
            type="date"
            format="YYYY/MM/DD"
            value-format="YYYY-MM-DD"
            placeholder="请选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="用户性别">
          <el-select v-model="form.lSex" placeholder="请选择性别">
            <el-option
              v-for="dict in sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="户主id" prop="lPillar">
          <el-input v-model="form.lPillar" placeholder="请输入户主id" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
            <div class="dialog-footer">
               <el-button type="primary" @click="submitForm">确 定</el-button>
               <el-button @click="cancel">取 消</el-button>
            </div>
       </template>
    </el-dialog>
  </div>
</template>

<script setup name="Friends">
import { listFriends, getFriends, delFriends, addFriends, updateFriends } from "@/api/life/friends";


const { proxy } = getCurrentInstance();
const { sys_user_sex} = proxy.useDict("sys_user_sex");

const loading = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const showSearch = ref(true);
const friendsList = ref([]);
const total = ref(0)
const title = ref("");
const open = ref(false);

const data = reactive({
  form: {},
  queryParams: {
        pageNum: 1,
        pageSize: 10,
        lRealName: null,
        lNickName: null,
        lPhoneNumber: null,
        lEmail: null,
        lIdCard: null,
        lBirthday: null,
        lSex: null,
        lPillar: null,
      },
      rules: {
        lRealName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
    },
});

const { queryParams, form, rules } = toRefs(data);

 /** 查询亲戚朋友列表 */
function  getList() {
  loading.value = true;
  listFriends(queryParams.value).then(response => {
    friendsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
// 取消按钮
function  cancel() {
    open.value = false;
    reset();
}
    // 表单重置
function  reset() {
  form.value = {
        lUserId: undefined,
        lRealName: undefined,
        lNickName: undefined,
        lPhoneNumber: undefined,
        lEmail: undefined,
        lIdCard: undefined,
        lBirthday: undefined,
        lSex: undefined,
        lPillar: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined
      };
      proxy.resetForm("friendsRef");
}
    /** 搜索按钮操作 */
function  handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }
/** 重置按钮操作 */
function  resetQuery() {
    proxy.resetForm("queryForm");
    handleQuery();
}
// 多选框选中数据
function   handleSelectionChange(selection) {
    ids.value = selection.map(item => item.lUserId)
    single.value = selection.length!==1
    multiple.value = !selection.length
}
/** 新增按钮操作 */
function  handleAdd() {
    reset();
    open.value = true;
    title.value = "添加亲戚朋友";
}
/** 修改按钮操作 */
function  handleUpdate(row) {
    reset();
    const lUserId = row.lUserId || ids.value
    getFriends(lUserId).then(response => {
      form.value = response.data;
      open.value = true;
      title.value = "修改亲戚朋友";
    });
  }
/** 提交按钮 */
function   submitForm() {
  proxy.$refs["friendsRef"].validate(valid => {
    if (valid) {
      if (form.value.lUserId != null) {
        updateFriends(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addFriends(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function  handleDelete(row) {
    const lUserIds = row.lUserId || ids.value;
    proxy.$modal.confirm('是否确认删除亲戚朋友编号为"' + lUserIds + '"的数据项？').then(function() {
      return delFriends(lUserIds);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {});
  }
    /** 导出按钮操作 */
function  handleExport() {
    proxy.download('life/friends/export', {
      ...queryParams.value
    }, `friends_${new Date().getTime()}.xlsx`)
  }
  
getList()
</script>
